<template>
    <!-- 添加 -->
    <div class="add-box">
      <i class="el-icon-edit"></i>
      <el-button @click="addUser">添加</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="编号" prop="id"> </el-table-column>
      <el-table-column label="秒杀时间段名称" prop="name"> </el-table-column>
      <el-table-column prop="startTime" label="每日开始时间">
        <template #default="scope">
          {{ fomatDate(scope.row.startTime) }}
        </template>
      </el-table-column>
      <el-table-column prop="endTime" label="每日结束时间">
        <template #default="scope">
          {{ fomatDate(scope.row.endTime) }}
        </template>
      </el-table-column>
      <el-table-column label="启用">
        <!-- 通过插槽得到的scope.row是每一列里面的 数据对象 -->
        <template #default="scope">
          <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" @change="changeHandle(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="text" @click="editHandle(scope.row.id)">编辑</el-button>
          <el-button type="text" @click="sureFn(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

  
    <addTime :show="showEditUser" @hide="hideDialog" @refresh="getList" :data="editId" />
  
    <AllocRole :show="showAllocRole" @hide="hideAlloc" :data="allocRoleId" />
  </template>
  
  <script lang="ts" setup>
  import addTime from "./components/addTime.vue";
  import { reactive, ref, toRefs } from "vue";
  import { user } from "@/api";
  import { fomatDate } from "@/utils";
  import { ElMessage, ElMessageBox, FormInstance } from "element-plus";
  import { Search } from "@element-plus/icons-vue";
  import { ElConfigProvider } from "element-plus";
  import zhCn from "element-plus/lib/locale/lang/zh-cn";
  import AllocRole from "./components/AllocRole.vue";
  import { useRouter } from "vue-router";
  
  const router = useRouter();
  interface IState {
    tableData: ItableDataUserParams[];

  }
  
  const state = reactive<IState>({
    tableData: []
  });
  
  // 数据
  const getList = () => {
    user.getflashSessionApi().then((res) => {
      if (res.code === 200) {
        console.log(res.data);
  
        state.tableData = res.data;
      }
    });
  };
  getList();

  // 分配角色
  const showAllocRole = ref(false);
  const allocRoleId = ref(0)
  
  // 分配角色隐藏
  const hideAlloc = () => {
    showAllocRole.value = false;
    allocRoleId.value = 0
  };
  
  const editId = ref();
  // 传值
  const editHandle = (id: number) => {
    const bj = tableData.value.findIndex(item => item.id === id)
    editId.value = tableData.value[bj];
    showEditUser.value = true;
  };
  // 用户状态的管理
  const changeHandle = (row: IUserModel) => {
    console.log(row.id, row.status);
  
    user
      .userflashSessionApi({ id: row.id, status: row.status })
      .then((res) => {
        ElMessage({
          message: res.message,
          type: res.code === 200 ? "success" : "error",
        });
      })
      .catch((err) => {
        row.status = row.status === 1 ? 0 : 1;
      });
  };
  // 重置
  const searchFormRef = ref<FormInstance>();
  const resetForm = () => {
    searchFormRef.value?.resetFields();
  };
  // addUser添加
  const showEditUser = ref(false);
  const addUser = () => {
    showEditUser.value = true;
  };
  // addUser取消
  const hideDialog = () => {
    showEditUser.value = false;
    // 还要把 editId 归 0
    editId.value = 0;
  };
  
  
  const sureFn = (id: number) => {
    ElMessageBox.confirm("是否要删除该时间段？", "提示", {
      type: "warning",
      confirmButtonText: "确定",
      cancelButtonText: "取消",
    }).then(() => {
      user.flashSessionApi(id).then((res) => {
        if (res.code === 200) {
          ElMessage.success("删除活动成功");
          getList();
        }
      });
    });
  
  };
  
  let { tableData } = toRefs(state);
  </script>
  
  <style lang="less" scoped>
  .search-box {
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 10px;
  
    .flex {
      display: flex;
      justify-content: space-between;
    }
  }
  
  .add-box {
    display: flex;
    justify-content: flex-end;
    padding: 10px;
    margin: 20px;
    border-radius: 10px;
    box-shadow: 0 0 4px 2px #ccc;
  }
  </style>
  